package cn.flying.cloud.flow.service.impl;

import javax.annotation.Resource;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.flow.bo.FwTaskCandidateBo;
import cn.flying.cloud.flow.entity.FwTaskCandidate;
import cn.flying.cloud.flow.mapper.FwTaskCandidateMapper;
import cn.flying.cloud.flow.service.FwProcNodeService;
import cn.flying.cloud.flow.service.FwTaskCandidateService;

/**
 * 流程节点权限信息表 服务实现类
 *
 * @author
 * @since 2024-09-20 10:45:05
 */
@Service
public class FwTaskCandidateServiceImpl extends ServiceImpl<FwTaskCandidateMapper, FwTaskCandidate> implements FwTaskCandidateService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private FwProcNodeService fwProcNodeService;

    /**
     * 查询总数
     *
     * @param param
     * @return
     */
    @Override
    public long countObj(FwTaskCandidateBo param) {
        return this.count(getLambdaQueryWrapper(param));
    }

    /**
     * 分页查询流程节点权限信息表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<FwTaskCandidateBo> getPageList(PageRequest<FwTaskCandidateBo> request) {
        FwTaskCandidateBo param = request.getParam();
        LambdaQueryWrapper<FwTaskCandidate> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        Page<FwTaskCandidate> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords().stream().map(FwTaskCandidate::toBO).collect(Collectors.toList()));
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<FwTaskCandidate> getLambdaQueryWrapper(FwTaskCandidateBo param) {
        LambdaQueryWrapper<FwTaskCandidate> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getProcDefId())) {
                wrapper.eq(FwTaskCandidate::getProcDefId, param.getProcDefId());
            }
            if (Ft.String.isNotBlank(param.getTenantId())) {
                wrapper.eq(FwTaskCandidate::getTenantId, param.getTenantId());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询流程节点权限信息表数据
     *
     * @param param
     * @return
     */
    @Override
    public List<FwTaskCandidateBo> getList(FwTaskCandidateBo param) {
        LambdaQueryWrapper<FwTaskCandidate> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        List<FwTaskCandidate> list = this.list(wrapper);
        return list.stream().map(FwTaskCandidate::toBO).collect(Collectors.toList());
    }

    /**
     * 查询流程节点权限信息表详情数据
     *
     * @param param
     * @return
     */
    @Override
    public FwTaskCandidateBo getOneObj(FwTaskCandidateBo param) {
        return this.getOne(new QueryWrapper<FwTaskCandidate>().lambda().eq(FwTaskCandidate::getProcDefId, param.getProcDefId())
                .eq(FwTaskCandidate::getNodeId, param.getNodeId())).toBO();
    }

    /**
     * 根据Id查询流程节点权限信息表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public FwTaskCandidateBo getObjById(Serializable id) {
        return this.getById(id).toBO();
    }

    /**
     * 新增流程节点权限信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(FwTaskCandidateBo param) {
        param.setProcDefKey(param.getProcDefId().split(":")[0]);
        param.setTenantId(Ft.Local.TenantId());
        FwTaskCandidate obj = new FwTaskCandidate();
        obj.fromBO(param);
        return this.save(obj);
    }

    /**
     * 更新流程节点权限信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(FwTaskCandidateBo param) {
        param.setProcDefKey(param.getProcDefId().split(":")[0]);
        FwTaskCandidate obj = new FwTaskCandidate();
        obj.fromBO(param);
        return this.saveOrUpdate(obj);
    }

    /**
     * 删除流程节点权限信息表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(FwTaskCandidateBo param) {
        LambdaQueryWrapper<FwTaskCandidate> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除流程节点权限信息表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
